package org.azj.logHelper;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;


class LogFile implements ILogger{

	private String LogSuffix = "\\Log\\";
	private String LogPath;
	private LOG_LEVEL DefaultLogLevel;
	private LOG_COMPONENT DefaultLogComponent;

	BufferedWriter bufferedWritter;
	boolean IsEnable = true;
	
	public LogFile(String fileName, LOG_LEVEL defaultLevel, LOG_COMPONENT defaultComponent) throws IOException
    {
        DefaultLogLevel = defaultLevel;
        DefaultLogComponent = defaultComponent;

        GenerateLogPath(fileName);

        // ��log�ļ�
        FileOutputStream writerStream = new FileOutputStream(LogPath);    
        bufferedWritter = new BufferedWriter(new OutputStreamWriter(writerStream, "UTF-8"));  
    }

    private void GenerateLogPath(String fileName)
    {
        // ��ȡ��ǰĿ¼
        String currentFolder = System.getProperty("user.dir");

        // �����־Ŀ¼
        String logFolder = currentFolder + LogSuffix;

        // ���Ŀ¼�Ƿ����
        File logDirectory = new File(logFolder);
        if (logDirectory.exists() == false)
        {
        	logDirectory.mkdir();
        }

        // ����log�ļ��ļ���
        LogPath = logFolder + String.format("%s_%s.log", fileName, extractDateString());
    }

	private String extractDateString() {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");  
		Calendar starttime = Calendar.getInstance();  
		String date = dateFormat.format(starttime.getTime());
		return date;
	}
	
	@Override
	public void Enable() {
		IsEnable = true;
	}

	@Override
	public void Disable() {
		IsEnable = false;
	}

	@Override
	public void SetLogLevel(LOG_LEVEL level) {
		DefaultLogLevel = level;
	}

	@Override
	public String GetLogFolderPath() {
		return LogPath;
	}

	//---------------------------------------------------------------------
    // Function: CLogFile::WriteLog()
    //
    // Description:
    //      Write log message to local file.
    //      
    //      Message Format:
    //      [Timestamp] [SPACE] [Logging Level] [SPACE] [Component] [SPACE] [Message]
	@Override
	public String WriteLog(LOG_LEVEL eLogLevel, LOG_COMPONENT component,
			String message) throws IOException {
		if (IsEnable)
        {
            String tmp = String.format("%s [%s] [%s] ",
            			extractDateString(),
                        eLogLevel,
                        component) + message + "\r\n";

            bufferedWritter.write(tmp);
            bufferedWritter.flush();

            return tmp;
        }
        else
        {
            throw new IOException("log is not editable");
        }
	}

	@Override
	public void WriteLog(String message)  throws IOException{
		WriteLog(DefaultLogLevel, DefaultLogComponent, message);
	}
	
	@Override
	public void DisposeLog() throws IOException{
        bufferedWritter.close();
	}
}
